package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_brand;

import cn.com.api.service.ApiT_brandService;

import cn.com.util.*;
import cn.com.util.*;

public class T_brandBaseController {

	private static Logger logger = Logger.getLogger(T_brandBaseController.class);

	@Autowired
	private ApiT_brandService apiT_brandService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_brand-list")
	public ModelAndView t_brand_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_brand_list.jsp");
		List<ApiT_brand> t_brands = apiT_brandService.findT_brands();
		if (null != t_brands && t_brands.size() > 0) {
			mv.addObject("t_brands", t_brands);
		}
		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_brand-add-pre")
	public ModelAndView t_brand_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_brand_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_brand t_brand = apiT_brandService.findT_brandById(Integer.valueOf(id));
			mv.addObject("t_brand", t_brand);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_brand-add")
	public void t_brand_add(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String u_id = request.getParameter("u_id");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String county = request.getParameter("county");
		String brand = request.getParameter("brand");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String phone = request.getParameter("phone");
		String name = request.getParameter("name");
		String enable = request.getParameter("enable");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_brand t_brand = new ApiT_brand();
			/*
			 * t_brand.setId(id); t_brand.setU_id(u_id);
			 * t_brand.setProvince(province); t_brand.setCity(city);
			 * t_brand.setCounty(county); t_brand.setbrand(brand);
			 * t_brand.setLongitude(longitude); t_brand.setLatitude(latitude);
			 * t_brand.setPhone(phone); t_brand.setName(name);
			 * t_brand.setEnable(enable);
			 */
			apiT_brandService.insertT_brand(t_brand);
		}
		// edit
		else {
			ApiT_brand t_brand = apiT_brandService.findT_brandById(Integer.valueOf(id));
			/*
			 * t_brand.setId(id); t_brand.setU_id(u_id);
			 * t_brand.setProvince(province); t_brand.setCity(city);
			 * t_brand.setCounty(county); t_brand.setbrand(brand);
			 * t_brand.setLongitude(longitude); t_brand.setLatitude(latitude);
			 * t_brand.setPhone(phone); t_brand.setName(name);
			 * t_brand.setEnable(enable);
			 */
			apiT_brandService.updateT_brand(t_brand);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_brand-delete")
	public void t_brand_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_brandService.deleteT_brand(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
